package wangyachuan.sptingboot.server;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import wangyachuan.sptingboot.Utils.TokenUtils;
import wangyachuan.sptingboot.common.page;
import wangyachuan.sptingboot.exception.ServiceException;
import wangyachuan.sptingboot.mapper.UserMapper;
import wangyachuan.sptingboot.model.user;


import java.util.List;


@SuppressWarnings("all")
@Service
public class UserServer {

    @Autowired
    UserMapper userMapper;// 自动注入UserMapper对象

    public void insertUser(user user) {
        userMapper.insert(user);// 调用userMapper的insert方法

    }

    // 根据id更新
    public void updateUser(user user) {
        userMapper.updateUser(user);// 调用userMapper的updateUser方法
    }

    // 根据id删除
    public void deleteUser(Integer id) {
        userMapper.deleteUser(id);// 调用userMapper的deleteUser方法
    }

    // 批量删除
    public void deleteUserBatch(List<Integer> ids) {
        for (Integer id : ids) {
            userMapper.deleteUser(id);// 调用userMapper的deleteuser方法
        }
    }

    // 查询所有
    public List<user> selectAll() {
        return userMapper.selectAll();// 调用userMapper的selectAll方法
    }

    // 根据id查询
    public user selectID(Integer id) {
        return userMapper.selectID(id);// 调用userMapper的selectID方法
    }

    // 条件查询
    public List<user> selectByName(String username) {
        return userMapper.getUserByName(username);// 调用userMapper的selectByName方法
    }

    // 多条件查询
    public List<user> selectByMore(String username, String name) {
        return userMapper.getUserByMore(username, name);// 调用userMapper的selectByMore方法
    }

    // 多条件查询
    public List<user> selectByMoreAndName(String username, String name) {
        return userMapper.getUserByMoreAndName(username, name);// 调用userMapper的selectByMoreAndName方法
    }

    //分页查询
    public page<user> selectByPage(Integer pageNum, Integer pageSize, String username, String name) {
        Integer skipNum = (pageNum - 1) * pageSize; // 计算起始位置，每页显示 pageSize 条记录
        page<user> page = new page<user>();
        List<user> userList = userMapper.getUserByPage(pageNum, pageSize, username, name);
        Integer total = userMapper.getUsercountByPage(username, name);
        page.setTotal(total);
        page.setList(userList);

        return page;
    }

    // 登录方法
    public user login(user user) {
        user userdb = userMapper.selectByName(user.getUsername());
        if (userdb == null) {
            //抛出一个异常
            throw new ServiceException("用户名或密码错误");
        }
        if (!user.getPassword().equals(userdb.getPassword())) {
            //抛出一个异常
            throw new ServiceException("用户名或密码错误");
        }
        //生成Token
        String token = TokenUtils.createToken(String.valueOf(userdb.getId()), userdb.getPassword());
        userdb.setToken(token);
        return userdb;
    }

    public user register(user user) {
        user userdb = userMapper.selectByName(user.getUsername());
        if (userdb != null) {
            //抛出一个异常
            throw new ServiceException("用户名已存在");
        }
        user.setName(user.getUsername());//将用户名作为姓名
        userMapper.insert(user);//作用是将用户信息插入数据库
        return user;
    }
}

